﻿using System;
using System.Data;
using System.Collections.Generic;
using Chenxin.Lib.Hr.Entity;
using Chenxin.Lib.Utility;
using System.Text;
namespace Chenxin.Lib.Hr.BLL
{
    /// <summary>
    /// 业务逻辑类t_Efficency_Employee 的摘要说明。
    /// </summary>
    public class t_Efficency_Employee
    {
        private readonly Chenxin.Lib.Hr.DAL.t_Efficency_Employee dal = new Chenxin.Lib.Hr.DAL.t_Efficency_Employee();
        public t_Efficency_Employee()
        { }
        #region  成员方法

        //Cache服务类
        private Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.HR);

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Chenxin.Lib.Hr.Entity.t_Efficency_Employee model)
        {
            string strEfficency = Chenxin.Lib.Cache.CacheDataType.HR_ALLDEPARTMENTS;
            cs.Delete(strEfficency);
            Chenxin.Lib.Hr.DAL.t_Efficency_Employee.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Chenxin.Lib.Hr.Entity.t_Efficency_Employee model)
        {
            string strEfficency = Chenxin.Lib.Cache.CacheDataType.HR_ALLDEPARTMENTS;
            cs.Delete(strEfficency);
            Chenxin.Lib.Hr.DAL.t_Efficency_Employee.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(Guid RecordID)
        {

            string strEfficency = Chenxin.Lib.Cache.CacheDataType.HR_ALLDEPARTMENTS;
            cs.Delete(strEfficency);
            Chenxin.Lib.Hr.DAL.t_Efficency_Employee.Delete(RecordID);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Chenxin.Lib.Hr.Entity.t_Efficency_Employee GetModel(Guid RecordID)
        {

            return Chenxin.Lib.Hr.DAL.t_Efficency_Employee.GetModel(RecordID);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return Chenxin.Lib.Hr.DAL.t_Efficency_Employee.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.t_Efficency_Employee> GetListArray(string strWhere)
        {
            DataSet ds = Chenxin.Lib.Hr.DAL.t_Efficency_Employee.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }

        public DataSet Proce(Guid CityID, Guid CompanyID, string YearMonth, string BeginDay, string EndDay, decimal DayNum)
        {
            return Chenxin.Lib.Hr.DAL.t_Efficency_Employee.Proce(CityID, CompanyID, YearMonth, BeginDay, EndDay, DayNum);
        }

        public DataSet EProce(Guid CityID, Guid CompanyID, string YearMonth, string BeginDay, string EndDay, decimal DayNum)
        {
            return Chenxin.Lib.Hr.DAL.t_Efficency_Employee.EProce(CityID, CompanyID, YearMonth, BeginDay, EndDay, DayNum);
        }

        public DataSet HProce(Guid CityID, Guid CompanyID, string YearMonth, string BeginDay, string EndDay, decimal DayNum)
        {
            return Chenxin.Lib.Hr.DAL.t_Efficency_Employee.HProce(CityID, CompanyID, YearMonth, BeginDay, EndDay, DayNum);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.t_Efficency_Employee> DataTableToList(DataTable dt)
        {
            List<Chenxin.Lib.Hr.Entity.t_Efficency_Employee> modelList = new List<Chenxin.Lib.Hr.Entity.t_Efficency_Employee>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Chenxin.Lib.Hr.Entity.t_Efficency_Employee model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Chenxin.Lib.Hr.Entity.t_Efficency_Employee();
                    if (dt.Rows[n]["RecordID"].ToString() != "")
                    {
                        model.RecordID = new Guid(dt.Rows[n]["RecordID"].ToString());
                    }
                    if (dt.Rows[n]["CityID"].ToString() != "")
                    {
                        model.CityID = new Guid(dt.Rows[n]["CityID"].ToString());
                    }
                    if (dt.Rows[n]["CompanyID"].ToString() != "")
                    {
                        model.CompanyID = new Guid(dt.Rows[n]["CompanyID"].ToString());
                    }
                    if (dt.Rows[n]["businessdistrictID"].ToString() != "")
                    {
                        model.businessdistrictID = new Guid(dt.Rows[n]["businessdistrictID"].ToString());
                    }
                    if (dt.Rows[n]["DepartID"].ToString() != "")
                    {
                        model.DepartID = new Guid(dt.Rows[n]["DepartID"].ToString());
                    }
                    if (dt.Rows[n]["EmployeeID"].ToString() != "")
                    {
                        model.EmployeeID = new Guid(dt.Rows[n]["EmployeeID"].ToString());
                    }
                    model.City = dt.Rows[n]["City"].ToString();
                    model.Company = dt.Rows[n]["Company"].ToString();
                    model.businessdistrict = dt.Rows[n]["businessdistrict"].ToString();
                    model.Departs = dt.Rows[n]["Departs"].ToString();
                    model.Employee = dt.Rows[n]["Employee"].ToString();
                    model.BeginTime = dt.Rows[n]["BeginTime"].ToString();
                    model.EndTime = dt.Rows[n]["EndTime"].ToString();
                    model.YearMonth = dt.Rows[n]["YearMonth"].ToString();
                    model.TransType = dt.Rows[n]["TransType"].ToString();
                    model.Position = dt.Rows[n]["Position"].ToString();
                    if (dt.Rows[n]["ChangType"].ToString() != "")
                    {
                        model.ChangType = int.Parse(dt.Rows[n]["ChangType"].ToString());
                    }
                    if (dt.Rows[n]["PeopleNum"].ToString() != "")
                    {
                        model.PeopleNum = decimal.Parse(dt.Rows[n]["PeopleNum"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        //*******************************************
        //功能概要       ：根据参数列表获得用户信息一览
        //参数説明       ：hsParams 参数列表
        //返回值説明     ：无
        //功能詳細       ：根据参数列表获得用户信息一览
        //作成者         ：zou.xiaochun
        //作成日         ：2010-09-28
        //*******************************************
        public DataSet GetModelListByCoditions(string strWhere, int strPageIndex, int strPageSize , string strYearMonth)
        {
            //排序
            string strOrderField = " Businessdistrict,Departs,Employee";
            //欲选择字段列表 
            string FieldList = " RecordId,City,Company,Businessdistrict,Departs,EmployeeCode,Employee,Position,BeginTime,EndTime,ChangType,TransType,PeopleNum,YearMonth,TotalNum ";
            //子查询欲选择字段列表
            StringBuilder sb = new StringBuilder();
            sb.Append(" a.RecordId,a.City,a.Company,a.Businessdistrict,a.Departs,(select UserCode from dbo.Hr_UserInfo WHERE UserId = a.EmployeeId) as EmployeeCode,a.Employee,a.Position,dateadd(dd,1,a.BeginTime) as BeginTime,dateadd(dd,0,a.EndTime) as EndTime,a.ChangType,a.TransType,cast(a.PeopleNum as decimal(5,2)) as PeopleNum,a.YearMonth,cast((cast(a.PeopleNum as decimal(5,2))*b.PeopleNum)as decimal(5,2)) as TotalNum ");
            //所要查询的表名
            StringBuilder TableName = new StringBuilder();
            TableName.AppendFormat(" (select RecordId,CityID,City,CompanyID,Company,BusinessdistrictID,Businessdistrict,DepartID,Departs,EmployeeID,Employee,PeopleNum,Position,BeginTime,EndTime,ChangType,TransType,YearMonth,ishow " +
                                "from t_Efficency_Employee where position in ('店长','区经','高经') and YearMonth = '{0}' " +
                                "group by RecordId,CityID,City,CompanyID,Company,BusinessdistrictID,Businessdistrict,DepartID,Departs,EmployeeID,Employee,PeopleNum,Position,BeginTime,EndTime,ChangType,TransType,YearMonth,ishow)a " +
                                "inner join (select c.DepartID,cast(sum(PeopleNum)as decimal(5,2)) as PeopleNum,c.YearMonth from " +
                                "(select DepartID,EmployeeID,PeopleNum,YearMonth from t_Efficency_Employee where isum = 1 and YearMonth = '{1}' " +
                                "group by DepartID,EmployeeID,PeopleNum,YearMonth)c group by c.DepartID,c.YearMonth)b on a.DepartID = b.DepartID ", strYearMonth, strYearMonth);
            string GroupField = " a.RecordId,a.City,a.Company,a.Businessdistrict,a.Departs,a.EmployeeId,a.Employee,a.Position,a.BeginTime,a.EndTime,a.ChangType,a.TransType,a.PeopleNum,a.YearMonth,b.PeopleNum ";
            //TableName.AppendFormat(" (select RecordId,CityID,City,CompanyID,Company,BusinessdistrictID,Businessdistrict,DepartID,Departs,EmployeeID,Employee,PeopleNum,Position,BeginTime,EndTime,ChangType,TransType,YearMonth " +
            //                    "from t_Efficency_Employee where position in ('店长','区经','高经') and YearMonth = '{0}' " +
            //                    "group by RecordId,CityID,City,CompanyID,Company,BusinessdistrictID,Businessdistrict,DepartID,Departs,EmployeeID,Employee,PeopleNum,Position,BeginTime,EndTime,ChangType,TransType,YearMonth)a " +
            //                    "inner join (select c.DepartID,cast(sum(PeopleNum)as decimal(5,2)) as PeopleNum,c.YearMonth from " +
            //                    "(select DepartID,EmployeeID,PeopleNum,YearMonth from t_Efficency_Employee where position='业务员' and YearMonth = '{1}' " +
            //                    "group by DepartID,EmployeeID,PeopleNum,YearMonth)c group by c.DepartID,c.YearMonth)b on a.DepartID = b.DepartID ", strYearMonth, strYearMonth);
            return Chenxin.Lib.Hr.DAL.t_Efficency_Employee.GetList(strPageIndex, strPageSize, strOrderField, TableName.ToString(), strWhere, GroupField, FieldList, sb.ToString(), true);
        }

        //*******************************************
        //功能概要       ：根据参数列表获得用户信息一览
        //参数説明       ：hsParams 参数列表
        //返回值説明     ：无
        //功能詳細       ：根据参数列表获得用户信息一览
        //作成者         ：zou.xiaochun
        //作成日         ：2010-09-28
        //*******************************************
        public DataSet GetModelByCoditions(string strWhere, int strPageIndex, int strPageSize)
        {
            //排序
            string strOrderField = " Businessdistrict,Departs,Employee";
            //欲选择字段列表 
            string FieldList = " RecordId,City,Company,Businessdistrict,Departs,EmployeeCode,Employee,Position,BeginTime,EndTime,ChangType,TransType,PeopleNum,YearMonth,TotalNum ";
            //子查询欲选择字段列表
            StringBuilder sb = new StringBuilder();
            sb.Append(" a.RecordId,a.City,a.Company,a.Businessdistrict,a.Departs,(select UserCode from dbo.Hr_UserInfo WHERE UserId = a.EmployeeId) as EmployeeCode,a.Employee,a.Position,dateadd(dd,1,a.BeginTime) as BeginTime,dateadd(dd,0,a.EndTime) as EndTime,a.ChangType,a.TransType,a.PeopleNum,a.YearMonth ");
            //所要查询的表名
            //StringBuilder TableName = new StringBuilder();
            //TableName.AppendFormat(" (select RecordId,CityID,City,CompanyID,Company,Businessdistrict,DepartID,Departs,EmployeeID,Employee,PeopleNum,Position,BeginTime,EndTime,ChangType,TransType,YearMonth " +
            //                    "from t_Efficency_Employee where position in ('店长','区经','高经') and recordid = '{0}'" +
            //                    "group by RecordId,CityID,City,CompanyID,Company,Businessdistrict,DepartID,Departs,EmployeeID,Employee,PeopleNum,Position,BeginTime,EndTime,ChangType,TransType,YearMonth)a " +
            //                    "inner join (select c.DepartID,cast(sum(PeopleNum)as decimal(5,2)) as PeopleNum,c.YearMonth from " +
            //                    "(select DepartID,EmployeeID,PeopleNum,YearMonth from t_Efficency_Employee where position='业务员' " +
            //                    "group by DepartID,EmployeeID,PeopleNum,YearMonth)c group by c.DepartID,c.YearMonth)b on a.DepartID = b.DepartID ", strRecordid);
            //string GroupField = " a.RecordId,a.City,a.Company,a.Businessdistrict,a.Departs,a.EmployeeId,a.Employee,a.Position,a.BeginTime,a.EndTime,a.ChangType,a.TransType,a.PeopleNum,a.YearMonth,b.PeopleNum ";
            string TableName = " t_Efficency_Employee a";
            string GroupField = " a.RecordId,a.City,a.Company,a.Businessdistrict,a.Departs,a.EmployeeId,a.Employee,a.Position,a.BeginTime,a.EndTime,a.ChangType,a.TransType,a.PeopleNum,a.YearMonth ";
            return Chenxin.Lib.Hr.DAL.t_Efficency_Employee.GetList(strPageIndex, strPageSize, strOrderField, TableName.ToString(), strWhere, GroupField, FieldList, sb.ToString(), true);
        }

        //*******************************************
        //功能概要       ：根据参数列表获得用户信息一览
        //参数説明       ：hsParams 参数列表
        //返回值説明     ：无
        //功能詳細       ：根据参数列表获得用户信息一览
        //作成者         ：zou.xiaochun
        //作成日         ：2010-09-28
        //*******************************************
        public DataSet GetEmpList(string strWhere, int strPageIndex, int strPageSize)
        {
            //排序
            string strOrderField = " Businessdistrict,Departs,Employee";
            //欲选择字段列表 
            string FieldList = " RecordId,City,Company,Businessdistrict,Departs,EmployeeCode,Employee,Position,BeginTime,EndTime,ChangType,TransType,PeopleNum,YearMonth ";
            //子查询欲选择字段列表
            StringBuilder sb = new StringBuilder();
            sb.Append(" a.RecordId,a.City,a.Company,a.Businessdistrict,a.Departs,(select UserCode from dbo.Hr_UserInfo WHERE UserId = a.EmployeeId) as EmployeeCode,a.Employee,a.Position,dateadd(dd,1,a.BeginTime) as BeginTime,dateadd(dd,0,a.EndTime) as EndTime,a.ChangType,a.TransType,a.PeopleNum,a.YearMonth ");
            //所要查询的表名
            string TableName = " t_Efficency_Employee a";
            string GroupField = " a.RecordId,a.City,a.Company,a.Businessdistrict,a.Departs,a.EmployeeId,a.Employee,a.Position,a.BeginTime,a.EndTime,a.ChangType,a.TransType,a.PeopleNum,a.YearMonth ";
            return Chenxin.Lib.Hr.DAL.t_Efficency_Employee.GetEmpList(strPageIndex, strPageSize, strOrderField, TableName, strWhere, GroupField, FieldList, sb.ToString(), true);
        }

        #endregion  成员方法
    }
}

